import React, { Component } from 'react';
import { WeaSearchPanel, WeaLocaleProvider } from 'ecCom';
import { inject, observer } from 'mobx-react';
import { Button, Row } from 'antd';
import { hashHistory } from 'react-router';

import SearchComponent from '../common/SearchComponent';

const getLabel = WeaLocaleProvider.getLabel;

@inject('centerStore')
@observer
class LayoutRightSearchComp extends Component {
  componentDidMount() {
    const { centerStore } = this.props;
    centerStore.buildSearchCondition();
  }

  render() {
    const { centerStore } = this.props;
    const { searchStore, getSearchsBaseValue, setSearchsBaseValue } = centerStore;
    return (
      <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@h0vxra`} className="wea-odoc-center-right-row">
        <WeaSearchPanel ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchPanel@bq9r1m`}
          searched={searchStore.searched}
          inputValue={getSearchsBaseValue()}
          onSearch={v => this.jumpToLibraryForOdocSubject(v)}
          onSearchChange={v => setSearchsBaseValue(v)}
          buttonsAd={this.renderSearchAdButtons()}
          searchsAd={<SearchComponent ecId={`${this && this.props && this.props.ecId || ''}_SearchComponent@glwui6`} searchStore={searchStore} listStore={centerStore} />}
          visible={searchStore.showSearchAd}
          onVisibleChange={v => searchStore.changeShowSearchAd(v)}
        />
      </Row>

    );
  }

  renderSearchAdButtons() {
    const { centerStore } = this.props;
    return [
      (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@2a1oc8@search`} type="primary" onClick={() => this.jumpToLibrary()}>{getLabel('197', '搜索')}</Button>),
      (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@efsm7p@reset`} type="ghost" onClick={() => centerStore.resetForm()}>{getLabel('2022', '重置')}</Button>),
      (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@sa30bo@cancel`}
        type="ghost"
        onClick={() => {
          centerStore.resetForm();
          centerStore.searchStore.changeShowSearchAd(false);
        }}
      >{getLabel('31129', '取消')}</Button>),
    ];
  }

  jumpToLibraryForAll() {
    const { centerStore } = this.props;
    let datas = centerStore.searchStore.form.getFormDatas();
    let odoctype = datas.odoctype.value[0];
    if (odoctype == '1') {
      let id = '';
      let name = '';
      if (datas.odoctype.value[1]) {
        id = datas.odoctype.value[1][0];
        name = datas.odoctype.value[1][1];
      }
      odoctype = `${odoctype}|${id}|${name}`;
    }

    let topictype = datas.topictype.value[0];
    if (topictype == '1') {
      let id = '';
      let name = '';
      if (datas.topictype.value[1]) {
        id = datas.topictype.value[1][0];
        name = datas.topictype.value[1][1];
      }
      topictype = `${topictype}|${id}|${name}`;
    }

    let writtendateselect = datas.writtendateselect.value[0];
    if (writtendateselect == '6' && datas.writtendateselect.value[1] && datas.writtendateselect.value[1][0] !== '' && datas.writtendateselect.value[1][1] !== '') {
      writtendateselect = `${writtendateselect}|${datas.writtendateselect.value[1][0]}|${datas.writtendateselect.value[1][1]}`;
    }

    this.jumpToLibrary(
      datas.odocSubject.value,
      odoctype,
      topictype,
      datas.dispatchnum.value,
      writtendateselect,
      datas.readstatus.value);
  }

  jumpToLibrary(odocSubject, odoctype, topictype, dispatchnum, writtendateselect, readstatus) {
    const { centerStore } = this.props;
    const formData = centerStore.searchStore.form.getFormParams();
    let params = {
      ...formData,
      odocSubject,
      odoctype,
      topictype,
      dispatchnum,
      writtendateselect,
      readstatus,
    };

    let path = {
      pathname: '/main/offical/odocLibrary',
      query: params,
    };

    hashHistory.push(path);
  }

  jumpToLibraryForOdocSubject(odocSubject) {
    this.jumpToLibrary(odocSubject);
  }
}

export default LayoutRightSearchComp;
